<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Tag extends Controller_Template {

    public function before()
    {
        parent::before();
        
        if ($this->auto_render === TRUE)
        {
            $this->title = '标签';
            $this->navigate->add('标签');
            
            $this->template->content_right(hook('sidebar'));
        }
    }

    public function after()
    {
        if ($this->auto_render === TRUE)
        {
        	;
        }
        
        parent::after();
    }
    
	public function action_index()
	{
		if ($tag = $this->request->param('id')){
            $tag = urldecode($tag);
            $this->title = $tag;
            
            $smarty = m::factory('smarty');
            if (!$smarty->isCached('index.tpl',md5('tag/'.$tag))){
                $o = MON::factory('tag')->findOne(array('tag'=>$tag));
                $list = Model::factory('tag')->get_articles($o['_id']);
                
                $this->pagination = new Pagination(array(
                    'total_items'=>$list->count()
                ));
                
                $list
				->skip($this->pagination->offset)
				->limit($this->pagination->items_per_page)
				->sort(array('addTime'=>-1));
                
                $smarty->assign('template',View::factory('index/default')
                ->set('list',$list)
                ->set('pagination',$this->pagination));
            }
            
            $this->content = $smarty->fetch('index.tpl',md5('tag/'.$tag));
		}else{
            lib_url::redirect('tag/top10');
		}
	}
	
	public function action_top10(){
		$this->title = '前十阅读';
		
		$smarty = m::factory('smarty');
		if (!$smarty->isCached('index.tpl',md5('top10'))){
            $read = Model::factory('article')->read_top();
            
            $list = array();
            foreach ($read as $_id=>$ips){
                $list[] = project::find_a($_id);
            }
            
            $smarty->assign('template',View::factory('index/default')
            ->set('list',$list)
            ->set('pagination',''));
		}
		
		$this->content = $smarty->fetch('index.tpl',md5('top10'));
	}
} // End Welcome
